define(function () {
    // dom query
    var doc = document

    function type(what) {
        return typeof what
    }

    function isString(what) {
        return type(what) === 'string'
    }

    function one(selector) {
        return isString(selector) ? doc.querySelector(selector) : selector
    }

    function all(selector) {
        return isString(selector) ? doc.querySelectorAll(selector) : selector
    }

    function attr(selector, attr, value) {
        var el = one(selector)
        if (type(value) === 'undefined') { // get
            value = el.getAttribute(attr) || ''
            return value && value.trim()
        }
        // set
        el.setAttribute(attr, value)
    }
    function data(selector, dataAttr, value) {
        var rtn = attr(selector, 'data-' + dataAttr, value)
        if (rtn) {
            try {
                rtn = JSON.parse(rtn)
            } catch (err) { }
            return rtn
        }
    }

    function hide(selector) {
        one(selector).style.display = 'none'
    }

    function show(selector) {
        one(selector).style.display = 'block'
    }

    function moveTo(selector, where) {
        one(selector).style.left = where.x + 'px'
        one(selector).style.top = where.y + 'px'
    }

    function getFormValues(form) {
        form = one(form)
        var inputs = form.querySelectorAll('[name]')
        var values = {}
        if (inputs.length) {
            each(inputs, function (item) {
                var name = item.name
                if (name) {
                    values[name] = item.value
                }
            })
        }
        return values
    }

    function setFormValues(form, values) {
        form = one(form)
        for (var name in values) {
            var input = form.querySelector('[name="' + name + '"]')
            if (input) {
                input.value = values[name] || ''
            }
        }
    }

    // log
    var debug
    function log(msg) {
        if (type(debug) === 'undefined') {
            debug = data('body', 'debug') === 'true'
        }
        if (!debug) return
        console.log(msg)
    }

    // lang helper
    function noop() { }

    function each(items, fn) {
        if (items && items.length) {
            for (var i = 0, len = items.length; i < len; i++) {
                var item = items[i]
                if (fn(item, i) === false) return
            }
        }
    }

    function nextName(prefix) { // 使用时间戳+随机值生成id
        prefix = prefix || 'c-'
        var time = Date.now().toString(36)
        var rnd = parseInt(Math.random() * 100) + 100
        return prefix + time + rnd
    }

    // export object
    return {
        doc: doc,
        type: type,
        isString: isString,
        one: one,
        all: all,
        attr: attr,
        data: data,
        hide: hide,
        show: show,
        moveTo: moveTo,
        getFormValues: getFormValues,
        setFormValues: setFormValues,
        log: log,
        noop: noop,
        each: each,
        nextName: nextName,
    }
})